package me.zhengjie.modules.quartz.task.pull;

import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.other.service.dto.CityCompanyDto;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.thread.ThreadPoolExecutorService;
import me.zhengjie.config.thread.ThreadPoolExecutorUtil;
import me.zhengjie.modules.quartz.service.dto.SyncCommon;
import me.zhengjie.modules.sync.PullService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Component
@Deprecated
public class SyncTask {

    //集团
    @Autowired
    private CorporationService corporationService;
    //城市公司
    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private PullService pullService;

    public void sync(){
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("deleted", match -> match.exact())
                .withIgnoreNullValues();
        Corporation corporation = new Corporation();
        corporation.setDeleted(Boolean.FALSE);
        Iterable<Corporation> list=corporationService.findAll(Example.of(corporation, matcher));
        list.forEach(item->{
            List<CityCompanyDto> cityCompanyDtoList= cityCompanyService.getCompanyList(item.getCorporationCode());
            if(null==cityCompanyDtoList)return;
            cityCompanyDtoList.stream().forEach(cityCompanyDto -> {
                if(cityCompanyDto.getStatus() == 1){
                    SyncCommon syncCommon=new SyncCommon();
                    syncCommon.setCorporationCode(item.getCorporationCode());
                    syncCommon.setAppId(item.getPullAppId());
                    syncCommon.setAppKey(item.getPullAppKey());
                    syncCommon.setDomain(item.getPullDomain());
                    //具体公司信息
                    syncCommon.setCityCode(cityCompanyDto.getCityCode());
                    syncCommon.setCompanyUuid(cityCompanyDto.getCompanyUuid());
                    //是否初始化
                    syncCommon.setInit(cityCompanyDto.getInitFlag());
                    pullService.setSyncCommon(syncCommon);
                    pullService.run();
                }

            });
        });

    }
}
